$(document).ready( function() {
	$('#bod-email').attr( 'alt', $_t( 'Tekst.UW_EMAILADRES', 'Uw e-mailadres' ) );
	$('#bod-prijs').attr( 'alt', '\u20AC' );
	
	$('#bod-email, #bod-prijs').blur( function() {
		if ( $(this).val() == '' ) {
			$(this).val( $(this).attr('alt') ).addClass( 'nonactief' );
		}
	} ).click( function() {
		$(this).removeClass( 'nonactief' ).val('');
	} ).trigger('blur');
	
	Carrousel.init();
	
	// Favoriet
	$('.opties .bewaren a').click( function() {
		if ( $(this).attr('href') == '#' ) {
			if ( jQuery.browser.msie ) {
				window.external.AddFavorite( $(location).attr('href').replace('#', ''), $(document).attr('title') ); 
			} else {
				$('#dialoog').dialog( 'option', 'title', $_t( 'Dialoog.Titel.FAVORIET_TOEVOEGEN', 'Favoriet toevoegen' ) ).html(
					$_t( 'Dialoog.Tekst.FAVORIET_TOEVOEGEN', 'Druk op CTRL+D om deze pagina aan uw favorieten toe te voegen.' )
				).dialog('open');
			}
		} else {
			$.ajax( {
				data: { opslaan: 'favoriet', nr: $.urlParam( $(this).attr('href'), 'favorite' ) },
				context: this,
				success: function( data ) {
					if ( typeof data == 'object' ) {
						$('#dialoog').dialog( 'option', 'title', $_t( 'Dialoog.Titel.FAVORIET_OPGESLAGEN', 'Favoriet opgeslagen' ) ).html(
							$_t( 'Dialoog.Tekst.FAVORIET_OPGESLAGEN', 'De advertentie is opgeslagen in uw <a href="/?rd=/user/favorites.html">favorietenlijst</a>.' )
						).dialog('open');
					}
					$(this).replaceWith( '<span>'+$_t( 'Tekst.OPGESLAGEN', 'Opgeslagen' )+'</span>' );
				}
			} );
		}
		
		return false;
	} );
	
	// Verwant
	if ( $.fn.qtip && !runLite ) {
		$('.verwant a').each( function(i) {
			if ( !verwantArray[i].ft ) {
				return false;
			}
			
			var html = '<img src="'+verwantArray[i].ft+'" /><br />'+verwantArray[i].pr;
			$(this).attr( 'title', '' ).qtip( {
				content: { text: html },
				position: {
					corner: { tooltip: 'rightMiddle', target: 'leftMiddle' },
					adjust: { x: -2 }
				},
				style: {
					'text-align': 'center',
					'background-color': '#fcfcfc',
					border: { width: 2, radius: 1, color: '#bebebe' },
					padding: 2,
					tip: { corner: 'rightMiddle', color: '#bebebe', size: { x: 8, y: 14 } }
				},
				hide: { effect: { length: 300 } }
			} );
		} );
	}
	
	// Referer
	$('.advertentie .feedback a[href*=/give-feedback.html]').click( function() {
		$(this).attr( 'href', $(this).attr( 'href' )+'&ref='+escape( document.location.href ) );
	} );
	
	// Locatie
	if ( !runLite ) {
		var locEle = $('.advertentie span[class^=locatie-]');
		if ( locEle.length == 1 ) {
			$('.adverteerder .opties').append('<li class="locatie"><a href="#" title="'+locEle.html()+'">Bekijk locatie</a></li>').find('.locatie a').click( function() {
				$('#dialoog').dialog( { title: locEle.html(), width: 900 } ).html( '<iframe src="/map.html?nr='+locEle.attr('class').replace('locatie-', '')+'" style="height: 500px; width: 100%; border: none;" frameborder="0" />' ).dialog('open');
				return false;
			} );
		}
	} else {
		$('.advertentie .adverteerder .opties').height( 40 );
	}
} );

var Carrousel = {
	parent: null, kader: null, volgende: null, vorige: null, groot: null,	
	eBreedte: 0, elementen: null, passendInKader: 0,
	
	init: function () {
		this.parent = $( '.carrousel' );
		if ( this.parent.length ) {
			this.kader = $( 'ul', this.parent );
			
			this.groot = $( '#foto-groot' );
			
			this.vorige = $( '.vorige', this.parent );
			this.volgende = $( '.volgende', this.parent );
			this.vorige.click( function() { return Carrousel.scroll('links'); } );
			this.volgende.click( function() { return Carrousel.scroll('rechts'); } );
			
			this.elementen = $( 'li', this.parent );
			this.elementen.bind( 'click mouseover', this.item );
			
			if ( this.elementen.length ) {
				this.eBreedte = Carrousel.elementen.outerWidth( true );
				this.passendInKader = Math.round( this.kader.width() / this.eBreedte );
			}
			this.scrollKnoppen();
		}
	},
	
	scroll: function( actie ) {
		var eActief = this.kader.find( 'li.actief' );
		var iActief = eActief.index();
		var iStap   = ( actie == 'links' ? -1 : 1 );
		
		if ( ( actie == 'links' && iActief <= 0 ) || ( actie == 'rechts' && iActief + iStap >= this.elementen.length ) ) {
			return false;
		}
		
		this.elementen.removeClass('actief');
		$( this.elementen[ iActief + iStap ] ).addClass( 'actief' ).trigger('click');
		
		if ( this.passendInKader < this.elementen.length ) {
			var itemPositie = ( ( iActief + iStap ) * this.eBreedte );
			if ( actie == 'rechts' ) {
				if ( itemPositie >= ( this.getPositie() + ( this.passendInKader * this.eBreedte ) ) ) {
					this.setPositie( this.getPositie() + this.eBreedte );
				}
			} else if ( itemPositie < this.getPositie() ) {
				this.setPositie( this.getPositie() - this.eBreedte );
			}
		}
		
		return false;
	},
	
	scrollKnoppen: function() {
		var iActief = this.kader.find( 'li.actief' ).index();
		
		if ( iActief == 0 ) {
			$(this.vorige).addClass( 'deactief' );
		}
		if ( iActief > 0 ) {
			$(this.vorige).removeClass( 'deactief' );
		}
		if ( iActief + 1 >= this.elementen.length ) {
			$(this.volgende).addClass( 'deactief' );
		}
		if ( iActief + 2 <= this.elementen.length ) {
			$(this.volgende).removeClass( 'deactief' );
		}
	},
	
	item: function() {
		$( Carrousel.elementen ).removeClass('actief');
		$(this).addClass('actief');
		$('a', this).trigger('blur');
		Carrousel.groot.attr( 'src', $('img', $(this)).attr('src').replace( '/small', '' ) );
		
		Carrousel.scrollKnoppen();
		
		return false;	
	},
	
	getPositie: function() {
		return Math.abs( parseInt( this.kader.css( 'marginLeft' ), 10 ) );
	},
	
	setPositie: function( links ) {
		this.kader.stop().animate( { marginLeft: ( links * -1 )+'px' }, 300 );
	}
};
